package com.coldwindblows.blog.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coldwindblows.blog.bo.RestResponseBo;
import com.coldwindblows.blog.constant.WebConst;
import com.coldwindblows.blog.controller.BaseController;
import com.coldwindblows.blog.dto.LogActions;
import com.coldwindblows.blog.dto.Types;
import com.coldwindblows.blog.pojo.ContentDo;
import com.coldwindblows.blog.pojo.UserDo;
import com.coldwindblows.blog.service.IContentService;
import com.coldwindblows.blog.service.ILogService;
import com.github.pagehelper.PageInfo;

/**
 * Created by ColdWindBlows on 2018/07/06
 *
 */
@Controller
@RequestMapping("admin/page")
@SuppressWarnings("rawtypes")
public class PageController extends BaseController {

	@Autowired
	private IContentService contentService;
	
	@Autowired
	private ILogService logService;
	
	@GetMapping("")
	public String index(Model model) {
		ContentDo temp = new ContentDo();
		temp.setType(Types.PAGE.getType());
		PageInfo<ContentDo> pageList = contentService.getArticlesWithPage(temp, 1, WebConst.MAX_PAGE);
		model.addAttribute("articles", pageList);
		
		return "admin/page_list";
	}
	
	@GetMapping("/new")
	public String newPage() {
		return "admin/page_list";
	}
	
	@GetMapping("/{cid}")
	public String editPage(@PathVariable String cid, Model model) {
		ContentDo content = contentService.getDetail(cid);
		model.addAttribute("contents", content);
		return "admin/page_edit";
	}
	
	@PostMapping("/publish")
	@ResponseBody
	public RestResponseBo publishPage(@RequestParam String title, @RequestParam String content, @RequestParam String status, @RequestParam String slug,@RequestParam(required = false) Integer allowComment,@RequestParam(required = false) Integer allowPing, HttpServletRequest request) {
		UserDo user = this.user(request);
		ContentDo temp = new ContentDo();
		temp.setTitle(title);
		temp.setContent(content);
		temp.setSlug(slug);
		temp.setStatus(status);
		temp.setType(Types.PAGE.getType());
		if(null != allowComment) {
			temp.setAllowComment(allowComment == 1);
		}
		if(null != allowPing) {
			temp.setAllowPing(allowPing == 1);
		}
		temp.setAuthorId(user.getUid());
		String result = contentService.publish(temp);
		if(!WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.fail(result);
		}
		return RestResponseBo.ok();
	}
	
	@PostMapping("/modify")
	@ResponseBody
	public RestResponseBo modifyArticle(@RequestParam Integer cid, @RequestParam String title, @RequestParam String content, @RequestParam String status, @RequestParam String slug,@RequestParam(required = false) Integer allowComment,@RequestParam(required = false) Integer allowPing, HttpServletRequest request) {
		UserDo user = this.user(request);
		ContentDo temp = new ContentDo();
		temp.setCid(cid);
		temp.setTitle(title);
		temp.setStatus(status);
		temp.setSlug(slug);
		temp.setContent(content);
		temp.setType(Types.PAGE.getType());
		if(null != allowComment) {
			temp.setAllowComment(allowComment == 1);
		}
		if(null != allowPing) {
			temp.setAllowPing(allowPing == 1);
		}
		
		temp.setAuthorId(user.getUid());
		String result = contentService.updateArticle(temp);
		if(!WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.fail(result);
		}
		return RestResponseBo.ok();
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public RestResponseBo delete(@RequestParam Integer cid, HttpServletRequest request) {
		String result = contentService.deleteContent(cid);
		logService.insertLog(LogActions.DEL_ARTICLE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
		if(WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.ok();
		}
		return RestResponseBo.fail(result);
	}
}
